package com.lzy.global.user_info;

import com.lzy.util.ThreadLocalUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author lzy 获取线程中绑定的全局属性
 */
public class CurrentThreadLoginUserInfo {
    /**
     * 当前程全局属性 用户id
     */
    public static final String USER_LOGIN_THREAD_KEY_USER_ID = "id";
    /**
     * 当前线程uuid
     */
    public static final String USER_LOGIN_REQUEST_UUID = "uuid";
    /**
     * 用户访问当前应用的appId
     */
    public static final String USER_LOGIN_THREAD_KEY_APP_ID = "appId";
    /**
     * 当前程全局属性 用户名称
     */
    public static final String USER_LOGIN_THREAD_KEY_USER_NAME = "name";
    /**
     * 当前程全局属性 com.lzy.interceptor.token
     */
    public static final String USER_LOGIN_THREAD_KEY_TOKEN = "com.lzy.interceptor.token";
    /**
     * 当前线程全局属性 手机号码
     */
    public static final String USER_LOGIN_THREAD_KEY_PHONE = "phone";

    /**
     * 当前线程全局属性 当前登录用户头像
     */
    public static final String USER_LOGIN_THREAD_KEY_HEAD_IMG = "headImg";
    /**
     * 用户类型
     */
    public static final String USER_LOGIN_THREAD_KEY_TYPE = "userType";
    /**
     * 用户所属租户id
     */
    public static final String USER_LOGIN_THREAD_KEY_TENANT = "tenantId";

    /**
     * 用户所属租户id
     */
    public static final String USER_LOGIN_THREAD_KEY_OPEN_ID = "openId";


    public static String getOpenId() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_OPEN_ID)).map(Object::toString).orElse(null);
    }

    /**
     * 值为hxxf_tenant库.t_tenant_member表的member_id
     */
    public static Long getUserId() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_USER_ID)).map(o -> Long.valueOf(o.toString())).orElse(null);
    }

    public static String getUserLoginRequestUuid(){
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_REQUEST_UUID)).map(Object::toString).orElse(null);
    }

    public static String getUserName() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_USER_NAME)).map(Object::toString).orElse(null);
    }

    public static String getToken() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_TOKEN)).map(Object::toString).orElse(null);
    }

    public static String getPhone() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_PHONE)).map(Object::toString).orElse(null);
    }

    public static String getHeadImg() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_HEAD_IMG)).map(Object::toString).orElse(null);
    }

    public static Integer getUserType() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_TYPE)).map(o -> Integer.valueOf(o.toString())).orElse(null);
    }

    public static Long getTenantId() {
        Map<String, Object> hashMap = getHashMap();
        return Optional.ofNullable(hashMap.get(USER_LOGIN_THREAD_KEY_TENANT)).map(o -> Long.valueOf(o.toString())).orElse(null);
    }

    private static Map<String, Object> getHashMap() {
        return Optional.ofNullable(ThreadLocalUtil.getObject()).orElse(new HashMap<>());
    }
}